<?php

class OrderInfoAction extends CommonAction {

    // 查询数据
    public function index() {
		//订单分类
		$this->assign('ordertypeHTML', $this->getOrderType() );
		//订单分类Select
		$this->assign('xiaoshouHTML' , $this->getXiaoShou() );	//销售Select
        $this->display();
    }
	
	public function indexjson() {
		$page = max(1, intval( $_POST['page'] ) );
		$rows = intval( $_POST['rows'] ) ? intval( $_POST['rows'] ) : 20;
		$limitstart = $rows*($page-1);
        $OrderInfo = D("OrderInfo");
        $list = $OrderInfo->limit( $limitstart , $rows)->order('id desc')->select();
		$row_tot = $OrderInfo->count();

		$i = 0;
		foreach($list as $row ){
			$orderinfo[$i]['table_id'] = $row['id'];
			$orderinfo[$i]['table_title'] = $row['title'];
			$orderinfo[$i]['table_code'] = 'XA-'.$row['id'];
			$orderinfo[$i]['table_brand']['brandid'] = $row['brandid'];
			$orderinfo[$i]['table_brand']['brandname'] = $row['brandname'] ? $row['brandname'] : '';
			$orderinfo[$i]['table_ordertype']['typeid'] = $row['typeid'];
			$orderinfo[$i]['table_ordertype']['typename'] = $row['typename'] ? $row['typename'] : '';
			$orderinfo[$i]['table_customer']['customerid'] = $row['customerid'];
			$orderinfo[$i]['table_customer']['customername'] = $row['customername'];
			$orderinfo[$i]['table_username'] = $row['username'];
			$orderinfo[$i]['table_usernameorder'] = $row['usernameorder'];
			$orderinfo[$i]['table_pubdate'] = date('Y-m-d',$row['pubdate']);
			$orderinfo[$i]['table_editdate'] = date('Y-m-d',$row['editdate']);
			$orderinfo[$i]['table_verifydate'] = $row['verifydate'] ? date('Y-m-d',$row['verifydate']) : '';
			$orderinfo[$i]['table_status'] = $row['status'];
			$orderinfo[$i]['table_trueprice'] = $row['trueprice'];
			$orderinfo[$i]['table_sysprice'] = $row_price['sysprice'];
			$orderinfo[$i]['table_offprice'] = $row_price['offprice'];
			$orderinfo[$i]['table_stime'] = $row_price['stime'];
			$orderinfo[$i]['table_etime'] = $row_price['etime'];
			
			$orderinfo[$i]['table_order'] = $row['id'];
		
		$i++;
		}
		$orderinfoHtml['total'] = $row_tot;
		$orderinfoHtml['rows'] = $orderinfo;
        echo json_encode($orderinfoHtml);
        
    }

    // 写入数据
    public function add() {
        $OrderInfo = D("OrderInfo");
        if ($vo = $OrderInfo->create()) {
            $list = $OrderInfo->add();
            if ($list !== false) {
                $this->success('数据保存成功！',U('index'));
            } else {
                $this->error('数据写入错误！');
            }
        } else {
            $this->error( 'ERR:'.$OrderInfo->getError().D()->_sql() );
        }
    }

    // 更新数据
    public function edit() {
        $OrderInfo = D("OrderInfo");
        if ($vo = $OrderInfo->create()) {
            $list = $OrderInfo->save();
            if ($list !== false) {
                $this->success('数据更新成功！',U('index'));
            } else {
                $this->error("没有更新任何数据!");
            }
        } else {
            $this->error($OrderInfo->getError());
        }
    }

    // 删除数据
    public function remove() {
		$id = intval( $_POST['form_id'] );
        if (!empty($id)) {
            $OrderInfo = M("OrderInfo");
			$vo = $OrderInfo->where("id='{$id}'")->find();
            if ($vo) {
				if( $vo['status'] == '0' || $vo['status'] == '-1'  ) {
					$result = $OrderInfo->delete($id);
					if (false !== $result) {
						$this->success('删除成功！');
					} else {
						$this->error('删除出错！');
					}
				}else {
					$this->error('此订单已经审核过，不能删除！');
				}
			}
        } else {
            $this->error('ID错误！');
        }
    }

    // 审核
    public function edit2() {
		$id = intval( $_POST['form_id'] );
        if ( !empty($id) ) {
            $OrderInfo = D("OrderInfo");
            $vo = $OrderInfo->where("id='{$id}'")->find();
            if ($vo) {
                if( $vo['status'] == 1 ) {
					$this->error('订单已经通过财务审核，不能再修改');
				}elseif ( $vo['status'] == 2 ) { 
					$this->error('此订单尚未通过审核，不能进行取消操作！');
				}elseif ( 0 == $vo['status'] ) {
					$data['status'] = 1;
					$data['verifydate'] = time();
					$v = $OrderInfo->where("id={$id}")->save( $data );
					if( $v ) {
						$this->success('通过审核');
					}else{
						$this->success('操作失败'.$OrderInfo->getError() );
					}
				}
            } else {
                $this->error('数据不存在！');
            }
        } else {
            $this->error('数据不存在！');
        }
    }
	/**
	* 取消审核
	**/
	function edit3() {
		$id = intval( $_POST['form_id'] );
        if ( !empty($id) ) {
            $OrderInfo = D("OrderInfo");
            $vo = $OrderInfo->where("id='{$id}'")->find();
            if ($vo) {
                if( $vo['status'] == '1' ) {
					$data['status'] = '0';
					$data['verifydate'] = '0';
					$v = $OrderInfo->where("id={$id}")->save( $data );
					if( $v ) {
						$this->success('成功取消审核一个订单！');
					}else{
						$this->success('操作失败'.$OrderInfo->getError() );
					}
				}else {
						$this->success('操作失败');
				}
            } else {
                $this->error('数据不存在！');
            }
        } else {
            $this->error('数据不存在！');
        }
	}
	/**
	* 排期
	**/
	function edit4() {
		$id = intval( $_POST['form_id'] );
        if ( !empty($id) ) {
            $OrderInfo = D("OrderInfo");
            $vo = $OrderInfo->where("id='{$id}'")->find();
            if ($vo) {
				$data['status'] = '-1';
				$data['verifydate'] = '0';
				$v = $OrderInfo->where("id={$id}")->save( $data );
				if( $v ) {
					$this->success('排期延时！');
				}else{
					$this->success('操作失败'.$OrderInfo->getError() );
				}
            } else {
                $this->error('数据不存在！');
            }
        } else {
            $this->error('数据不存在！');
        }
	}
	/**
	* 取消排期
	**/
	function edit5() {
		$id = intval( $_POST['form_id'] );
        if ( !empty($id) ) {
            $OrderInfo = D("OrderInfo");
            $vo = $OrderInfo->where("id='{$id}'")->find();
            if ($vo) {
				$data['status'] = '0';
				$data['verifydate'] = '0';
				$v = $OrderInfo->where("id={$id}")->save( $data );
				if( $v ) {
					$this->success('取消排期延时！');
				}else{
					$this->success('操作失败'.$OrderInfo->getError() );
				}
            } else {
                $this->error('数据不存在！');
            }
        } else {
            $this->error('数据不存在！');
        }
	}
	
	/**
	* 销售列表
    * @todo 添加过滤条件 role = $roleid
	**/
	public function getXiaoShou() {
		$xiaoshou = M("User")->select();
		if( $xiaoshou ) {
			foreach( $xiaoshou as $row ) {
				$usernameorderSelect[] = '<option value="'.$row['nickname'].'">'.$row['nickname'].'</option>';
			}
		}
		return $usernameorderSelect ? implode(',',$usernameorderSelect) : '';
	
	}
	
	public function getOrderType ( $pid=0, $suojin='' ) {
		$OrderType = D("OrderType");
		$list = $OrderType->where("pid='{$pid}' and status='0'")->order('sort ASC')->select();
		if( $list ) {
			foreach( $list as $row ) {
				$ordertypeSelect[] = '<option value="'.$row['id'].'">'.$suojin.$row['name'].'</option>';
				$list_sub = $OrderType->where("pid='{$row['id']}' and status='0'")->count();
				if( $list_sub ) {
					$ordertypeSelect[] =  $this->getOrderType( $row['id'], '──');
				}
			}
		}
		return $ordertypeSelect ? implode(',',$ordertypeSelect) : '';
	}
	
	/**
	* 客户查询
	**/
	public function customer () {
		$page = max(1, intval( $_POST['page'] ) );
		$rows = intval($_POST['rows']) ? intval($_POST['rows']) : 10;
		$limitstart = $rows*($page-1);
		$searchtitle = trim( htmlspecialchars( $_POST['searchtitle']) );
		$searchaddress = trim( htmlspecialchars( $_POST['$searchaddress']) );
		$searchcontact = trim( htmlspecialchars( $_POST['searchcontact']) );
		$searchphone = trim( htmlspecialchars($_POST['searchphone']) );
		$sqladd = "";
		if($searchtitle){
			$sqladd .= " AND title LIKE '%".$searchtitle."%' ";
		}
		if($searchaddress){
			$sqladd .= " AND address LIKE '%".$searchaddress."%' ";
		}
		if($searchprovince){
			$sqladd .= " AND province='".$searchprovince."' ";
		}
		if($searchphone){
			$sqladd .= " AND ( phone LIKE '%".$searchphone."%' OR mobile LIKE '%".$searchphone."%' OR email LIKE '%".$searchphone."%') ";
		}
		if($searchtypeid){
		$sqladd .= " AND  typeid='".$searchtypeid."' ";
		}
		if($noshowdel=='1'){
			$sqladd .= " AND  status='0' ";
		}else{
			if($_SESSION['_admin_ismycustomer']=='1'){
			$sqladd .= " AND a.userid='".$_SESSION['_admin_userid']."' ";
			}
		}
		$customer = array();

		$row_tot = D('CustomerView')->where( $sqladd )->count();
		$customers = D('CustomerView')->where( $sqladd )->limit( $limitstart.','.$rows)->select();
		$i = 0;
		//while($row = $db->fetch_array($query)){
		foreach ( $customers as $row ) {
		$customer[$i]['table_id'] = $row['id'];
		$customer[$i]['table_title'] = $row['title'];
		$customer[$i]['table_shorttitle'] = $row['shorttitle'];
		$customer[$i]['table_address'] = $row['address'];
		$customer[$i]['table_province']['id'] = $row['province'];
		$customer[$i]['table_province']['name'] = $globalProvinceArray[$row['province']];
		$customer[$i]['table_contact'] = $row['contact'];
		$customer[$i]['table_phone'] = $row['phone'];
		$customer[$i]['table_mobile'] = $row['mobile'];
		$customer[$i]['table_email'] = $row['email'];
		$customer[$i]['table_typeid']['id'] = $row['typeid'];
		$customer[$i]['table_typeid']['name'] = $row['typename'];
		$customer[$i]['table_status'] = $row['status'];
		$i++;
		//}
		}
		$customerHtml['total'] = $row_tot;
		$customerHtml['rows'] = $customer;
		echo json_encode($customerHtml);
	}
}